#include <pbstdlib.h>
#include "define.h"

#define MAXV  100 

void delay_ms( uint16_t n) {
    uint16_t start =   ((unsigned)__input(TIM_BASEADDR+1)<<8)| __input(TIM_BASEADDR) ;
    uint16_t end ;
    start = start + n ;

    for(;;) {
        end = ((unsigned)__input(TIM_BASEADDR+1)<<8)| __input(TIM_BASEADDR) ;
        if(end > start) {
            break  ;
        }
    }
}


int main()
{
    int8_t s = 1;
    int8_t cnt = 0 ; 
    int8_t pwm= 0 ;

    uint16_t start =  ((unsigned)__input(TIM_BASEADDR+1)<<8)| __input(TIM_BASEADDR) ;
    uint16_t end = 0 ;
    while(1) {
        cnt = cnt<MAXV? cnt+1 : 0 ;
        __output(GPIO_BASEADDR, cnt<pwm); 

        end = ((unsigned)__input(TIM_BASEADDR+1)<<8)| __input(TIM_BASEADDR) ;
        if(end > start) {
            start = end + 5;
            pwm = pwm + s ;
            s = (pwm==MAXV)&&(s==1) ? -1 : (pwm==0)&&(s==-1) ? 1 : s;
        }
    }

    return 0 ;
}
